package binaryTree.LayerOrderTraversal;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Basic515 {
//查找每一层的最大值
    public List<Integer> largestValues(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        LinkedList<TreeNode> que = new LinkedList<>();
        if (root==null){
            return list;
        }
        que.offerLast(root);
        while (!que.isEmpty()){
            TreeNode treeNode = que.peekFirst();
            int max= treeNode.val;
            int size = que.size();
            while (size>0){
                TreeNode  pop = que.pollFirst();
                if (max< pop.val){
                    max= pop.val;
                }
              if (pop.left!=null){
                 que.offerLast(pop.left);
              }
                if (pop.right!=null){
                    que.offerLast(pop.right);
                }
                size--;

            }
            list.add(max);
        }

        return  list;
    }

}
